package com.cyn.ningxia;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

public class WebSocketClientExample {
 
    public static void main(String[] args) {
        try {
            for (int j = 1; j <= 10; j++) {
                for (int i = 1; i <= 1000; i++) {
                    int finalI1 = i;

                    int finalJ = j;
                    new Thread(()->{
                        ddd(finalJ +"*"+finalI1);
                    }).start();


                }
                Thread.sleep(30000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void ddd(String finalI1){
        URI uri = null; // 替换为你的WebSocket服务器URI
        try {
            uri = new URI("ws://192.168.109.132:18080/websocket/"+ finalI1);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        String finalI = finalI1;
        WebSocketClient webSocketClient = new WebSocketClient(uri) {
            @Override
            public void onOpen(ServerHandshake handshakedata) {
                System.out.println("新连接已打开"+ finalI);
            }

            @Override
            public void onMessage(String message) {
                System.out.println("接收到消息: " + message);
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                System.out.println("连接已关闭"+code);

            }

            @Override
            public void onError(Exception ex) {
                ex.printStackTrace();
            }
        };

        webSocketClient.connect(); // 连接到WebSocket服务器
    }
}